package org.jadapter.tests;

import static org.junit.Assert.assertEquals;

import org.jadapter.JAdapter;
import org.jadapter.annotations.Adaptable;
import org.jadapter.examples.foobar.Bar;
import org.jadapter.examples.foobar.Foo;
import org.jadapter.examples.foobar.FooBar;
import org.jadapter.examples.foobar.FooBean;
import org.jadapter.registry.TransitiveAdapterRegistry;
import org.jadapter.tests.examples.AnnotatedFoo;
import org.jadapter.weavers.ConfigurableWeaver;
import org.junit.Test;

public class TestConfigurableWeaver {

	
	@Test public void shouldProxySpecifiedClasses() {

        TransitiveAdapterRegistry registry = new TransitiveAdapterRegistry(
                new JAdapter(FooBar.class)
        );

        ConfigurableWeaver weaver = new ConfigurableWeaver(registry, registry); 
        weaver.setClasses(new Class[]{FooBean.class});
     
        Foo proxy = weaver.weave(new FooBean(7));
        
        Bar bar = (Bar)proxy;
          
        assertEquals(8, bar.bar());
        
    }
    
    
    @Test(expected=ClassCastException.class)
	public void shouldNotProxyUnspecifiedClasses() {

        TransitiveAdapterRegistry registry = new TransitiveAdapterRegistry(
                new JAdapter(FooBar.class)
        );

        ConfigurableWeaver weaver = new ConfigurableWeaver(registry, registry); 
        weaver.setClasses(new Class[]{FooBean.class});
        
        Foo proxy = weaver.weave(new AnnotatedFoo());
        
        // FooBean is not annotated, resulting in a normal class cast exception
        Bar bar = (Bar)proxy; 
        
    }
    
    
    
    @Test
	public void shouldProxySpecifiedAnnotationMarkers() {

        TransitiveAdapterRegistry registry = new TransitiveAdapterRegistry(
                new JAdapter(FooBar.class)
        );

        ConfigurableWeaver weaver = new ConfigurableWeaver(registry, registry); 
        weaver.setAnnotations(new Class[]{Adaptable.class});
        
        Foo proxy = weaver.weave(new AnnotatedFoo());
        
        Bar bar = (Bar)proxy;
        
        assertEquals(8, bar.bar());
        
    }
    

       
    @Test
	public void shouldProxySpecifiedInterfaceMarkers() {

        TransitiveAdapterRegistry registry = new TransitiveAdapterRegistry(
                new JAdapter(FooBar.class)
        );

        ConfigurableWeaver weaver = new ConfigurableWeaver(registry, registry); 
        weaver.setInterfaces(new Class[]{Foo.class});
        
        Foo proxy = weaver.weave(new AnnotatedFoo());
        
        Bar bar = (Bar)proxy;
        
        assertEquals(8, bar.bar());
        
    }
	
}
